$(document).ready(
    function() {
        showTipMsgs();
        obtainType();
        $("#licenseTemplate-id").on("change", function() {
            $("#licenseTemplate-type").val($(this).children('option:selected').text());
        });

        $("#inputForm")
            .validate(
                {
                    ignore: "",
                    submitHandler : function(form) {
                        form.submit();
                    },
                    rules:{
                        'licenseMain.subject':{
                            licenseName:true
                        },
                        'ip':{
                            chineseCheck:true
                        },
                        'disk':{
                            chineseCheck:true
                        },
                        'mac':{
                            chineseCheck:true
                        },
                        'cpu':{
                            chineseCheck:true
                        },

                    },
                    errorElement:'div',
                    errorPlacement : function(error, element) {
                        $(element).parent().next().remove();
                        if (element.is(":checkbox")
                            || element.is(":radio")
                            || element.parent().is(
                                ".input-append")) {
                            error.appendTo(element.parent()
                                .parent());
                            $("<label class='wrong fa fa-times' style='margin-left: 5px;line-height: 34px'></label>").insertAfter(element.parent().parent().parent());
                        } else {
                            error.insertAfter(element);
                            $("<label class='wrong fa fa-times' style='margin-left: 5px;line-height: 34px'></label>").insertAfter(element.parent());
                        }
                    },
                    success: function(element) {
                        $(element).parent().next().removeClass('wrong fa fa-times');
                        $(element).parent().next().addClass('right fa fa-check');
                        $(element).remove();
                    },
                });
    });

/*验证license名称*/
jQuery.validator.addMethod("licenseName",function (value, element) {
    var regex =  /[0-9]+[a-zA-Z]+[0-9a-zA-Z]*|[a-zA-Z]+[0-9]+[0-9a-zA-Z]*/;
    return this.optional(element)  || regex.test(value);
},"license名称必须是英文加字母组合！");

/*验证license的ip,DISK,CPU*/
jQuery.validator.addMethod("chineseCheck",function (value, element) {
    var regex =  /[^，\x22]/;
    return this.optional(element)  || regex.test(value);
},"内容中不能包含中文逗号！");



function obtainType(){
    $.ajax({
        url:'../license/license_licenseTemplate',
        async: false,
        dataType: "json",
        success:function(data) {
            con = "";
            for (var i in data) {
                if (i == 0) {
                    $("#licenseTemplate-type").val(data[0].type);
                }
                con += "<option value="+data[i].id+">"+data[i].type+"</option>";
                $("#licenseTemplate-id").html(con);
            }
        }
    });
}
function  submitLicense() {
    $.ajax({
        type: "POST",
        url:'../license/save',
        async: false,
        data:$('#inputForm').serialize(),
        error: function(xml, obj) {
            console.log(xml);
            console.log(obj);
        },
        success: function(data) {
            var result = JSON.parse(data);
            if (result.isSuccess) {
                alert("license证书创建成功!");
            } else {
                alert(result.msg);

            }

        }
    });
}

function f_list_page() {
    layer.open({
        type: 2,
        title: 'license创建成功提示页面',
        shadeClose: true,
        maxmin: true,
        shade: 0.8,
        area: ['50%', '85%'],
        content: 'license_buidSuccessPage',
        cancel: function (index) {
            layer.close(index);
        },
        end: function (index) {
            layer.close(index);

        }
    });
}


